package diguidfs;
import java.util.*;
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
         this.right = right;
      }
  }
public class Text1 {
    //力扣题目 257 : 二叉树的所有路径
    //算法 : dfs  剪枝  回溯
    //定义一个全局变量  记录 所有的路径
    List<String> ret = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        //深度遍历  剪枝   回溯
        dfs(root,new StringBuffer());
        return ret;
    }

    public void dfs(TreeNode root,StringBuffer buffer) {
        if(root == null) {
            return;
        }
        StringBuffer tmp = new StringBuffer(buffer);

        tmp.append(Integer.toString(root.val));
        if(root.left == null && root.right == null) {
            //存入表中
            ret.add(tmp.toString());
            return;
        }
        tmp.append("->");
        dfs(root.left,tmp);
        dfs(root.right,tmp);
    }
}
